package com.controller;

import java.io.*;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

import com.bean.User;
import com.dao.UserSQL;
import com.service.MD5;


/**
 * Servlet implementation class Login
 */
@WebServlet("/Login")
public  class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;
	public Login() {
		super();
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session = request.getSession();
		String name =request.getParameter("name");
		String password = request.getParameter("password");
		User user;
		if(password==""||name=="")
			session.setAttribute("state1", "请输入用户名与密码");
		else if ((user=new UserSQL().queryUser(name))  != null) {
			if (user.getPassword().equals(MD5.stringMD5(password))) {// 登录验证
				session.setAttribute("pass", user);
			} else {
				session.setAttribute("state1", "密码错误"); 
			}
		} else {
			session.setAttribute("state1", "用户不存在");
			
		}
		response.sendRedirect("login.jsp");
		
	}

	public void init(ServletConfig config) throws ServletException {
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session=request.getSession();
		String action = request.getParameter("action");//判断action
		if ("leave".equals(action)) {// 退出登录
			session.invalidate();
			response.sendRedirect("login.jsp");
			return;
		}  
	}

}
